Python Directory in Hindi: mkdir, listdir, scandir सीखें
Python Directory क्या है? Folders बनाना, देखना, Rename और Delete करना
Python में files के साथ काम करना सीखने के बाद अगला जरूरी step है directories को संभालना। Real projects में data अलग-अलग folders में रखा जाता है, जैसे notes, images, reports और backup। इन folders को हर बार हाथ से बनाने या देखने की जगह आप Python code से यह काम कर सकते हैं।
इस tutorial में आप सीखेंगे कि directory क्या होती है और os.getcwd(), os.listdir(), os.mkdir(), os.makedirs(), os.path.exists(), os.path.isdir(), os.path.isfile(), os.chdir(), os.rename(), os.rmdir() और os.scandir() कैसे इस्तेमाल होते हैं। हर example के साथ possible Terminal output और VS Code Explorer में दिखने वाला folder structure भी दिया गया है।
यह lesson Python os Module in Hindi का directory-focused practical version है। अगर आपने file handling की basics नहीं पढ़ीं, तो पहले Python File Handling in Hindi: Core Concepts & Workflow और Python File Writing in Hindi देखें।
os module से directory operations पर focus है। pathlib, shutil, os.walk(), file permissions और recursive folder deletion को detail में नहीं पढ़ाया गया है।
Directory क्या होती है?
Directory (आम भाषा में folder) एक ऐसी जगह है जिसमें files और दूसरी directories रखी जा सकती हैं। किसी directory के अंदर बनी directory को subdirectory या subfolder कहते हैं।
ऊपर PythonProject एक directory है। उसके अंदर दो files (main.py, notes.txt) और एक subdirectory (practice) है, जिसमें आगे दो files हैं।
Directory Operations की जरूरत क्यों पड़ती है?
- Project के लिए folder structure अपने-आप तैयार करना।
- किसी folder की files और subfolders की list देखना।
- File और folder को अलग-अलग पहचानना।
- Program की working location बदलना।
- Folder का नाम बदलना।
- खाली folder हटाना।
- Reports, backups और downloads जैसे folders को organize रखना।
os.rename() और os.rmdir() वाले examples पहले अपने practice folder में चलाएँ। किसी जरूरी folder का path इन functions को न दें।os.getcwd() — Current Working Directory
os.getcwd() बताता है कि Python program इस समय किस directory में काम कर रहा है। Relative paths, जैसे "notes.txt", इसी directory के आधार पर resolve होते हैं।
Terminal Output (Possible):
आपके computer पर path अलग होगा।
os.getcwd() print करके देखें।os.listdir() — Folder Contents देखना
os.listdir() किसी directory में मौजूद files और folders के नामों की list देता है। Path न दें, तो यह current working directory की list देता है।
Terminal Output (Possible):
Output आपके folder की वास्तविक content पर निर्भर करेगा।
os.listdir() की list का क्रम guaranteed नहीं होता। क्रम चाहिए तो sorted() इस्तेमाल करें। यह function सिर्फ नाम देता है, इसलिए नाम देखकर आप नहीं बता सकते कि entry file है या folder।os.mkdir() — नया Folder बनाना
os.mkdir() एक नई directory बनाता है। इसकी parent directory पहले से मौजूद होनी चाहिए। उसी नाम की directory पहले से हो, तो FileExistsError आ सकती है।
Terminal Output (पहली बार):
Terminal Output (दोबारा run करने पर):
os.makedirs() — Nested Directories बनाना
कई levels वाले folders एक साथ बनाने हों, तो os.makedirs() इस्तेमाल करें। जरूरत पड़ने पर यह बीच के parent folders भी बना सकता है।
Terminal Output:
exist_ok=True होने पर target directory पहले से मौजूद होने पर केवल उसी कारण से error नहीं आती, इसलिए यह code दोबारा चलाया जा सकता है।
| Function | कब इस्तेमाल करें |
|---|---|
os.mkdir() | सिर्फ एक folder बनाना हो और parent पहले से मौजूद हो। |
os.makedirs() | Nested folders एक साथ बनाने हों। |
os.path.exists() — Path Check करना
os.path.exists(path) बताता है कि दिए गए path पर कुछ मौजूद है या नहीं। वह file हो या directory, दोनों के लिए True मिलता है।
os.path.isdir() और os.path.isfile()
exists() सिर्फ इतना बताता है कि कुछ मौजूद है। यह जानने के लिए कि वह folder है या file, इन दोनों functions का इस्तेमाल होता है:
os.path.isdir(path)— path मौजूद directory है या नहीं।os.path.isfile(path)— path मौजूद regular file है या नहीं।
Terminal Output (Possible):
os.chdir() — Current Directory बदलना
os.chdir() program की current working directory बदलता है। इसके बाद के सभी relative paths नई directory से resolve होते हैं। इसलिए बदलने से पहले पुरानी directory को variable में सुरक्षित रखना अच्छा तरीका है।
Terminal Output (Possible):
os.chdir() से FileNotFoundError आ सकती है। Chdir के बाद अगर notes.txt जैसी file नहीं मिल रही, तो संभव है आप गलत working directory में हैं।os.rename() — Directory Rename करना
os.rename(old, new) सिर्फ files के लिए नहीं, directories के लिए भी काम करता है। पुरानी directory मौजूद होनी चाहिए।
Terminal Output (Possible):
यहाँ code पहले दोनों चीजें check करता है, यानी पुरानी directory मौजूद है और नया नाम पहले से इस्तेमाल में नहीं है। इससे गलती से किसी जरूरी चीज से टकराव का खतरा कम होता है।
os.rmdir() — Empty Directory Delete करना
os.rmdir() सिर्फ खाली directory हटाता है। अगर directory में कोई file या subfolder है, तो operation fail होता है और OSError आ सकती है।
Terminal Output:
इस lesson में सिर्फ खाली directory हटाने का तरीका दिखाया गया है। किसी बड़े folder को recursively हटाने के लिए अलग tools और अतिरिक्त सावधानी की जरूरत होती है। जरूरी folders पर कभी बिना जाँच के delete न चलाएँ।
os.scandir() — Directory Entries देखना
os.scandir() किसी directory की entries को DirEntry objects के रूप में देता है। हर entry पर name, path, is_file() और is_dir() जैसी जानकारी मिलती है। जब आपको file और folder को अलग-अलग पहचानना हो, तो यह os.listdir() से ज्यादा सुविधाजनक है।
Terminal Output (Possible):
यहाँ with statement का इस्तेमाल इसलिए हुआ है कि scandir() का iterator काम खत्म होने पर ठीक से बंद हो जाए। sorted(..., key=lambda e: e.name) entries को नाम के अनुसार क्रम में रखता है, क्योंकि scandir() भी किसी निश्चित क्रम की गारंटी नहीं देता।
| Feature | os.listdir() | os.scandir() |
|---|---|---|
| क्या मिलता है | नामों की list | DirEntry objects |
| File/folder पहचान | अलग से os.path.isdir() चाहिए | entry.is_dir(), entry.is_file() |
| Full path | os.path.join() से बनाना पड़ता है | entry.path |
| कब अच्छा है | सिर्फ नामों की जरूरत हो | नाम के साथ type भी चाहिए |
Practical Example — Folder Structure Setup
अब पिछले functions को मिलाकर एक छोटा program बनाते हैं। यह PythonProject के अंदर notes, images और reports folders बनाएगा और फिर उनकी जानकारी print करेगा।
Terminal Output (Possible):
Windows पर path में backslash दिख सकता है।
Mini Project — Workspace Directory Report
अब एक mini project बनाते हैं। यह program MyWorkspace नाम का workspace बनाएगा, उसमें चार folders तैयार करेगा, दो sample files लिखेगा और फिर हर folder में कितनी entries हैं, इसकी report दिखाएगा। इसमें makedirs(), os.path.join(), open(), scandir(), listdir() और is_dir() सभी का इस्तेमाल है।
Terminal Output (Possible):
यह program दोबारा चलाने पर भी error नहीं देगा, क्योंकि folders exist_ok=True से बन रहे हैं। हाँ, sample files फिर से लिखी जाएँगी, यानी उनकी पुरानी content replace हो जाएगी। इसलिए इसे सिर्फ practice workspace में चलाएँ।
Common Mistakes
Folder पहले से मौजूद हो, तो FileExistsError आ सकती है।
अगर Projects या Projects/Python मौजूद नहीं है, तो FileNotFoundError आ सकती है।
सिर्फ नाम से पता नहीं चलता कि entry file है या folder। Folder का नाम भी data.v1 जैसा हो सकता है।
या फिर os.scandir() और entry.is_dir() इस्तेमाल करें।
os.rmdir() सिर्फ खाली directory हटाता है। Directory में content हो, तो OSError आ सकती है।
os.chdir() पूरे program की working directory बदल देता है। उसके बाद relative paths अलग जगह से resolve होते हैं और files मिलना बंद हो सकती हैं।
Destination path पहले से मौजूद हो, तो behavior operating system के अनुसार अलग हो सकता है और error आ सकती है।
Quick Exercise 🧠
Nested directories (जैसे Projects/Python/Files) एक साथ बनाने के लिए कौन सा function सही है?
os.mkdir()os.rmdir()os.makedirs()सही उत्तर: C — os.makedirs()
Final Coding Challenge
अब VS Code में directory_practice.py नाम की file बनाएँ और नीचे दिए गए tasks पूरा करें। Rename और delete वाले tasks के लिए केवल अपनी बनाई हुई practice directories का इस्तेमाल करें।
os.getcwd()से current directory print करें।os.mkdir()सेDirPracticeबनाएँ; पहले existence check जरूर करें।os.makedirs()सेDirPractice/Level1/Level2बनाएँ।os.path.exists()औरos.path.isdir()से दोनों folders check करें।- एक text file
DirPractice/info.txtबनाएँ औरos.path.isfile()से उसे check करें। os.listdir()सेDirPracticeकी entries print करें।os.scandir()से हर entry को "Folder" या "File" label के साथ print करें।os.chdir()सेDirPracticeमें जाएँ,os.getcwd()print करें और फिर पुरानी directory में वापस आएँ।Level2कोos.rename()सेLevel2_Renamedकरें।Level2_Renamedखाली है, इसलिए उसेos.rmdir()से हटाएँ।- Bonus: VS Code Explorer खोलकर हर step के बाद अपना folder structure verify करें।
Quick Revision
| Function | काम |
|---|---|
os.getcwd() | Current working directory बताता है। |
os.listdir(path) | Directory की entries के नाम list में देता है। |
os.mkdir(path) | एक directory बनाता है। |
os.makedirs(path, exist_ok=True) | Nested directories बना सकता है। |
os.path.exists(path) | Path मौजूद है या नहीं check करता है। |
os.path.isdir(path) | Path directory है या नहीं check करता है। |
os.path.isfile(path) | Path regular file है या नहीं check करता है। |
os.chdir(path) | Current working directory बदलता है। |
os.rename(old, new) | Directory या file rename करता है। |
os.rmdir(path) | खाली directory delete करता है। |
os.scandir(path) | Directory entries को DirEntry objects के रूप में देता है। |
FAQ — Python Directory Operations
1. Python में directory क्या होती है?
Directory को आम भाषा में folder कहते हैं। यह एक container है जिसमें files और दूसरी directories (subfolders) रखी जा सकती हैं। Python में os module से directories बनाई, देखी, बदली, rename और delete की जा सकती हैं।
2. Directory और folder में क्या फर्क है?
Programming और file system की भाषा में इन्हें आमतौर पर एक ही चीज़ माना जाता है। Windows और VS Code जैसे tools में इसे folder कहा जाता है, जबकि technical documentation में अक्सर directory शब्द इस्तेमाल होता है।
3. os.getcwd() क्या करता है?
os.getcwd() current working directory का path string के रूप में देता है। यह वही directory होती है जिसे Python program relative paths के लिए आधार मानता है।
4. os.listdir() और os.scandir() में क्या अंतर है?
os.listdir() entries के नामों की list देता है, जबकि os.scandir() हर entry के लिए DirEntry object देता है, जिस पर name, path, is_file() और is_dir() जैसे attributes और methods मिलते हैं। File और folder को अलग करना हो, तो os.scandir() ज्यादा सुविधाजनक है।
5. os.mkdir() और os.makedirs() में क्या अंतर है?
os.mkdir() एक directory बनाता है और उसकी parent directory पहले से मौजूद होनी चाहिए। os.makedirs() जरूरत पड़ने पर बीच की सभी parent directories भी बना सकता है।
6. os.makedirs() में exist_ok=True का क्या मतलब है?
exist_ok=True देने पर अगर target directory पहले से मौजूद है, तो सिर्फ इसी कारण FileExistsError नहीं आती। इससे code दोबारा चलाना आसान हो जाता है।
7. Directory मौजूद है या नहीं, कैसे check करें?
os.path.exists(path) से path मौजूद होने की जाँच होती है और os.path.isdir(path) से यह पता चलता है कि वह path directory है या नहीं।
8. os.chdir() से directory बदलने के बाद क्या ध्यान रखना चाहिए?
os.chdir() पूरे program की current working directory बदल देता है, इसलिए उसके बाद के सभी relative paths उसी नई directory से resolve होंगे। बदलने से पहले os.getcwd() से पुरानी directory सुरक्षित रख लेना अच्छा तरीका है।
9. os.rmdir() non-empty directory क्यों delete नहीं करता?
os.rmdir() केवल खाली directory हटाने के लिए बना है। Directory में files या subfolders हों, तो OSError आ सकती है। यह एक तरह की सुरक्षा भी है, ताकि गलती से पूरा folder tree न हट जाए।
10. Directory rename करने के लिए कौन सा function इस्तेमाल होता है?
os.rename(old_name, new_name) से directory का नाम बदला जा सकता है। पुरानी directory मौजूद होनी चाहिए और नए नाम का path पहले से किसी जरूरी चीज़ से टकराना नहीं चाहिए।
Summary
Python Directory Operations = os module से folders बनाना, देखना, बदलना, rename करना और खाली folders हटाना।
इस lesson में आपने directory का मतलब, os.getcwd(), os.listdir(), os.mkdir(), os.makedirs(), os.path.exists(), os.path.isdir() और os.path.isfile(), os.chdir(), os.rename(), os.rmdir() और os.scandir() सीखे, साथ ही एक mini project भी बनाया।
अब Final Coding Challenge पूरा करें और हर step के बाद VS Code Explorer में अपना folder structure verify करें। जब यह comfortable लगे, तो modern तरीके के लिए Python pathlib Module in Hindi lesson पढ़ें।
Related Articles — Python Hindii
- Python os Module in Hindi — Files, Folders और Directories Manage करें
- Python pathlib Module in Hindi — Path Object से Files और Folders Manage करें
- Python File Handling in Hindi: Core Concepts & Workflow
- Python File Reading in Hindi
- Python File Writing in Hindi — Create Files & write()
- Python Exception Handling in Hindi
- Python Strings in Hindi
- Python Lists Complete Guide in Hindi
- Python For Loop in Hindi
- Python Tutorial in Hindi — Complete Roadmap
- Online Python Editor & Compiler in Hindi
Python code likho, run karo aur practice karo — directly browser mein.
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें